package com.shenhaofeng.app.component.fragment;

import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.util.Pair;

import java.util.ArrayList;
import java.util.List;


/**
 * 带有标题的ViewPager的Fragment适配器
 * Created by Administrator on 2016/4/1.
 */
public class FragmentPagerTitleAdapter extends FragmentPagerAdapter {


    private final List<Pair<CharSequence, Fragment>> fragments = new ArrayList<>();


    public FragmentPagerTitleAdapter(FragmentManager fm) {
        super(fm);
    }


    @Override
    public Fragment getItem(int position) {
        Pair<CharSequence, Fragment> pair = fragments.get(position);
        return pair.second;
    }


    @Override
    public int getCount() {
        return fragments.size();
    }


    @Override
    public CharSequence getPageTitle(int position) {
        return fragments.get(position).first;
    }


    /**
     * 添加一组fragment到适配器中
     *
     * @param title
     * @param fragment
     */
    public FragmentPagerTitleAdapter addFragment(CharSequence title, Fragment fragment) {
        fragments.add(new Pair<>(title, fragment));
        notifyDataSetChanged();
        return this;
    }


}
